//
//  AccountManager.m
//  CarpoolWithFriends
//
//  Created by Kai Wang on 5/29/12.
//  Copyright 2012 __MyCompanyName__. All rights reserved.
//

#import "AccountManager.h"

@interface AccountManager()

-(id)initSingleton;
@end

@implementation AccountManager

- (id) initSingleton
{
    if ((self = [super init]))
    {
        // Initialization code here.
    }
    
    return self;
}

+ (AccountManager *) instance
{
    // Persistent instance.
    static AccountManager *_default = nil;
    
    // Small optimization to avoid wasting time after the
    // singleton being initialized.
    if (_default != nil)
    {
        return _default;
    }
    
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_4_0
    // Allocates once with Grand Central Dispatch (GCD) routine.
    // It's thread safe.
    static dispatch_once_t safer;
    dispatch_once(&safer, ^(void)
                  {
                      _default = [[AccountManager alloc] initSingleton];
                  });
#else
    // Allocates once using the old approach, it's slower.
    // It's thread safe.
    @synchronized([AccountManager class])
    {
        // The synchronized instruction will make sure,
        // that only one thread will access this point at a time.
        if (_default == nil)
        {
            _default = [[AccountManager alloc] initSingleton];
        }
    }
#endif
    return _default;
}

- (id) retain
{
    return self;
}

- (oneway void) release
{
    // Does nothing here.
}

- (id) autorelease
{
    return self;
}

- (NSUInteger) retainCount
{
    return INT32_MAX;
}

@end